home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / emacs-18.59src.lha / emacs-18.59 / lisp / hideif.el < prev    next >
Encoding:
Text File  |  1992-11-21  |  31.8 KB  |  1,051 lines

  1. ;;; hide-ifdef-mode.el   Hides selected code within ifdef.
  2. ;;;
  3. ;;; Copyright (C) 1988 Brian Marick and Daniel LaLiberte
  4. ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
  5. ;;; Extensively modified by Daniel LaLiberte (while at Gould).
  6. ;;;
  7. ;;; You may freely modify and distribute this, but keep a record
  8. ;;; of modifications and send comments to:
  9. ;;;      liberte@a.cs.uiuc.edu  or  ihnp4!uiucdcs!liberte
  10. ;;; I will continue to upgrade hide-ifdef-mode
  11. ;;; with your contributions and will eventually offer it to FSF.
  12. ;;;
  13. ;;; $Header: Workbench2.0:unix/usr/lib/emacs-18.59/lispr: Workbench2.0:unix/u 1.2.1.1 1992/11/21 15:55:33 dgay Exp $
  14. ;;;
  15. ;;; $Log: hideif.el,v $
  16. ; Revision 1.2.1.1  1992/11/21  15:55:33  dgay
  17. ; Amiga patches
  18. ;
  19. ; Revision 1.2  1992/11/15  13:29:56  dgay
  20. ; emacs18.59
  21. ;
  22. ; Revision 1.1  1992/11/15  10:09:42  dgay
  23. ; Initial revision
  24. ;
  25. ;;; Revision 1.7  88/02/16  03:12:58  liberte
  26. ;;; Fixed comments and doc strings.
  27. ;;; Added optional prefix arg for ifdef motion commands.
  28. ;;; 
  29. ;;; Revision 1.6  88/02/05  00:36:18  liberte
  30. ;;; Bug fixes.
  31. ;;; 1. A multi-line comment that starts on an #ifdef line
  32. ;;;    now ends on that line.
  33. ;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only
  34. ;;; 3. Make ifdef-block hiding work outside of ifdefs.
  35. ;;; 
  36. ;;; Revision 1.5  88/01/31  23:19:31  liberte
  37. ;;; Major clean up.
  38. ;;;   Prefix internal names with "hif-".
  39. ;;; 
  40. ;;; Revision 1.4  88/01/30  14:09:38  liberte
  41. ;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist.
  42. ;;; 
  43. ;;; Revision 1.3  88/01/29  00:38:19  liberte
  44. ;;; Fix three bugs.
  45. ;;; 1. Function "defined" is just like lookup.
  46. ;;; 2. Skip to newline or cr in case text is hidden.
  47. ;;; 3. Use car of token list if just one symbol.
  48. ;;;
  49. ;;; Revision 1.2  88/01/28  23:32:46  liberte
  50. ;;; Use hide-ifdef-mode-prefix-key.
  51. ;;; Copy current-local-map so other buffers do not get
  52. ;;; hide-ifdef-mode bindings.
  53. ;;;
  54. ;;;--------------------------------------------------------------
  55. ;;; To initialize, toggle the hide-ifdef minor mode with
  56. ;;;
  57. ;;; M-x hide-ifdef-mode
  58. ;;;
  59. ;;; This will set up key bindings and call hide-ifdef-mode-hook if it
  60. ;;; has a value.  To explicitly hide ifdefs using a buffer-local
  61. ;;; define list (default empty), type
  62. ;;;
  63. ;;; M-x hide-ifdefs  or C-c h
  64. ;;;
  65. ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
  66. ;;; pass through.  The support of constant expressions in #if lines is 
  67. ;;; limited to identifiers, parens, and the operators: &&, ||, !, and
  68. ;;; "defined".  Please extend this.
  69. ;;;
  70. ;;; The hidden code is marked by ellipses (...).  Be
  71. ;;; cautious when editing near ellipses, since the hidden text is
  72. ;;; still in the buffer, and you can move the point into it and modify
  73. ;;; text unawares.  If you don't want to see the ellipses, set 
  74. ;;; selective-display-ellipses to nil.  But this can be dangerous.
  75. ;;; You can make your buffer read-only while hide-ifdef-hiding by setting
  76. ;;; hide-ifdef-read-only to a non-nil value.  You can toggle this 
  77. ;;; variable with hide-ifdef-toggle-read-only (C-c C-q).
  78. ;;;
  79. ;;; You can undo the effect of hide-ifdefs by typing
  80. ;;;
  81. ;;; M-x show-ifdefs  or C-c s
  82. ;;;
  83. ;;; Use M-x hide-ifdef-define (C-c d) to define a symbol.
  84. ;;; Use M-x hide-ifdef-undef (C-c u) to undefine a symbol.
  85. ;;;
  86. ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
  87. ;;; the display will be updated.  Only the define list for the current
  88. ;;; buffer will be affected.  You can save changes to the local define
  89. ;;; list with hide-ifdef-set-define-alist.  This adds entries 
  90. ;;; to hide-ifdef-define-alist.
  91. ;;;
  92. ;;; If you have defined a hide-ifdef-mode-hook, you can set
  93. ;;; up a list of symbols that may be used by hide-ifdefs as in the
  94. ;;; following example:
  95. ;;;
  96. ;;; (setq hide-ifdef-mode-hook
  97. ;;;      '(lambda ()
  98. ;;;     (if (not hide-ifdef-define-alist)
  99. ;;;         (setq hide-ifdef-define-alist
  100. ;;;          '((list1 ONE TWO)
  101. ;;;            (list2 TWO THREE)
  102. ;;;            )))
  103. ;;;     (hide-ifdef-use-define-alist 'list2) ; use list2 by default
  104. ;;;     ))
  105. ;;;
  106. ;;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
  107. ;;; another list to use.
  108. ;;;
  109. ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
  110. ;;; set hide-ifdef-initially to non-nil.
  111. ;;;
  112. ;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
  113. ;;; In the absence of highlighting, that might be a bad idea.  If you set
  114. ;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
  115. ;;; lines will be displayed.  That can be confusing in its own
  116. ;;; right.  Other variations on display are possible, but not much
  117. ;;; better.
  118. ;;;
  119. ;;; You can explicitly hide or show individual ifdef blocks irrespective
  120. ;;; of the define list by using hide-ifdef-block and show-ifdef-block.
  121. ;;;
  122. ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
  123. ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
  124. ;;;
  125. ;;; If you have minor-mode-alist in your mode line (the default) two labels
  126. ;;; may appear.  "Ifdef" will appear when hide-ifdef-mode is active.  "Hiding"
  127. ;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
  128.  
  129.  
  130.  
  131. (defvar hide-ifdef-mode-map nil
  132.   "Keymap used with hide-ifdef mode")
  133.  
  134. (defconst hide-ifdef-mode-prefix-key "\C-c"
  135.   "Prefix key for all hide-ifdef-mode commands.")
  136.  
  137. (defvar hide-ifdef-mode-map-before nil
  138.   "Buffer-local variable to store a copy of the local keymap
  139.     before hide-ifdef-mode modifies it.")
  140.  
  141. (defun define-hide-ifdef-mode-map ()
  142.   (if hide-ifdef-mode-map
  143.       ()                ; dont redefine it.
  144.     (setq hide-ifdef-mode-map (make-sparse-keymap))
  145.     (define-key hide-ifdef-mode-map "d" 'hide-ifdef-define)
  146.     (define-key hide-ifdef-mode-map "u" 'hide-ifdef-undef)
  147.     (define-key hide-ifdef-mode-map "D" 'hide-ifdef-set-define-alist)
  148.     (define-key hide-ifdef-mode-map "U" 'hide-ifdef-use-define-alist)
  149.   
  150.     (define-key hide-ifdef-mode-map "h" 'hide-ifdefs)
  151.     (define-key hide-ifdef-mode-map "s" 'show-ifdefs)
  152.     (define-key hide-ifdef-mode-map "\C-h" 'hide-ifdef-block)
  153.     (define-key hide-ifdef-mode-map "\C-s" 'show-ifdef-block)
  154.   
  155.     (define-key hide-ifdef-mode-map "\C-f" 'forward-ifdef)
  156.     (define-key hide-ifdef-mode-map "\C-b" 'backward-ifdef)
  157.     (define-key hide-ifdef-mode-map "\C-d" 'down-ifdef)
  158.     (define-key hide-ifdef-mode-map "\C-u" 'up-ifdef)
  159.     (define-key hide-ifdef-mode-map "\C-n" 'next-ifdef)
  160.     (define-key hide-ifdef-mode-map "\C-p" 'previous-ifdef)
  161.     (define-key hide-ifdef-mode-map "\C-q" 'hide-ifdef-toggle-read-only)
  162.     (define-key hide-ifdef-mode-map
  163.       (where-is-internal 'toggle-read-only nil t)
  164.       'hide-ifdef-toggle-outside-read-only)
  165.     )
  166.   (fset 'hide-ifdef-mode-map hide-ifdef-mode-map)  ; the function is the map
  167.   )
  168.  
  169. (defun hif-update-mode-line ()
  170.   "Update mode-line by setting buffer-modified to itself."
  171.   (set-buffer-modified-p (buffer-modified-p)))
  172.  
  173.  
  174. (defvar hide-ifdef-mode nil
  175.   "non-nil when hide-ifdef-mode is activated.")
  176.  
  177. (defvar hide-ifdef-hiding nil
  178.   "non-nil when text may be hidden.")
  179.  
  180. (or (assq 'hide-ifdef-hiding minor-mode-alist)
  181.     (setq minor-mode-alist
  182.           (cons '(hide-ifdef-hiding " Hiding")
  183.                 minor-mode-alist)))
  184.  
  185. (or (assq 'hide-ifdef-mode minor-mode-alist)
  186.     (setq minor-mode-alist
  187.           (cons '(hide-ifdef-mode " Ifdef")
  188.                 minor-mode-alist)))
  189.  
  190.  
  191. (defun hide-ifdef-mode (arg)
  192.   "Toggle hide-ifdef-mode.  Thus this is a minor mode, albeit a large one.
  193. With arg, turn hide-ifdef-mode on iff arg is positive.
  194. In hide-ifdef-mode, code within #ifdef constructs that the C preprocessor
  195. would eliminate may be hidden from view.  Several variables affect
  196. how the hiding is done:
  197.  
  198. hide-ifdef-env
  199.     An association list of defined and undefined symbols for the
  200.     current buffer.  Initially, the global value of hide-ifdef-env is used.
  201.  
  202. hide-ifdef-define-alist
  203.     An association list of defined symbol lists.  
  204.         Use hide-ifdef-set-define-alist to save the current hide-ifdef-env
  205.         and hide-ifdef-use-define-alist to set the current hide-ifdef-env
  206.         from one of the lists in hide-ifdef-define-alist.
  207.  
  208. hide-ifdef-lines
  209.     Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
  210.     #endif lines when hiding.
  211.  
  212. hide-ifdef-initially
  213.     Indicates whether hide-ifdefs should be called when hide-ifdef-mode
  214.     is activated.
  215.  
  216. hide-ifdef-read-only
  217.     Set to non-nil if you want to make buffers read only while hiding.
  218.     After show-ifdefs, read-only status is restored to previous value.
  219.  
  220. \\{hide-ifdef-mode-map}"
  221.  
  222.   (interactive "P")
  223.   (make-local-variable 'hide-ifdef-mode)
  224.   (setq hide-ifdef-mode
  225.     (if (null arg)
  226.         (not hide-ifdef-mode)
  227.       (> (prefix-numeric-value arg) 0)))
  228.   
  229.   (hif-update-mode-line)
  230.  
  231.   (if hide-ifdef-mode
  232.       (progn
  233.     ; fix c-mode syntax table so we can recognize whole symbols.
  234.     (modify-syntax-entry ?_ "w")
  235.     (modify-syntax-entry ?& ".")
  236.     (modify-syntax-entry ?\| ".")
  237.  
  238.     ; inherit global values
  239.     (make-local-variable 'hide-ifdef-env)
  240.     (setq hide-ifdef-env (default-value 'hide-ifdef-env))
  241.  
  242.     (make-local-variable 'hide-ifdef-hiding)
  243.     (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
  244.  
  245.     (make-local-variable 'hif-outside-read-only)
  246.     (setq hif-outside-read-only buffer-read-only)
  247.  
  248.     (make-local-variable 'ide-ifdef-mode-map-before)
  249.     (setq hide-ifdef-mode-map-before (current-local-map))
  250.     (use-local-map (copy-keymap (current-local-map)))
  251.     (local-unset-key hide-ifdef-mode-prefix-key)
  252.     (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map)
  253.     (define-hide-ifdef-mode-map)
  254.  
  255.     (run-hooks 'hide-ifdef-mode-hook)
  256.  
  257.     (if hide-ifdef-initially
  258.         (hide-ifdefs)
  259.       (show-ifdefs))
  260.     (message "Enter hide-ifdef-mode.")
  261.     )
  262.      ; else end hide-ifdef-mode
  263.     (if hide-ifdef-hiding
  264.     (show-ifdefs))
  265.     (use-local-map hide-ifdef-mode-map-before)
  266.     (message "Exit hide-ifdef-mode.")
  267.     ))
  268.   
  269.  
  270. ;; from outline.el with docstring fixed.
  271. (defun hif-outline-flag-region (from to flag)
  272.   "Hides or shows lines from FROM to TO, according to FLAG.  If FLAG
  273. is \\n (newline character) then text is shown, while if FLAG is \\^M
  274. \(control-M) the text is hidden."
  275.   (let ((modp (buffer-modified-p)))
  276.     (unwind-protect (progn
  277.               (subst-char-in-region from to
  278.                   (if (= flag ?\n) ?\^M ?\n)
  279.                   flag t) )
  280.       (set-buffer-modified-p modp))
  281.     ))
  282.  
  283. (defun hif-show-all ()
  284.   "Show all of the text in the current buffer."
  285.   (interactive)
  286.   (hif-outline-flag-region (point-min) (point-max) ?\n))
  287.  
  288. (defun hide-ifdef-region (start end)
  289.   "START is the start of a #if or #else form.  END is the ending part.
  290. Everything including these lines is made invisible."
  291.   (hif-outline-flag-region start end ?\^M)
  292.   )
  293.  
  294. (defun hif-show-ifdef-region (start end)
  295.   "Everything between START and END is made visible."
  296.   (hif-outline-flag-region start end ?\n)
  297.   )
  298.  
  299.  
  300.  
  301. ;===%%SF%% evaluation (Start)  ===
  302.  
  303. (defvar hide-ifdef-evaluator 'eval
  304.   "The evaluator is given a canonical form and returns T if text under
  305. that form should be displayed.")
  306.  
  307. (defvar hif-undefined-symbol nil
  308.   "...is by default considered to be false.")
  309.  
  310. (defvar hide-ifdef-env nil
  311.   "An alist of defined symbols and their values.")
  312.  
  313.  
  314. (defun hif-set-var (var value)
  315.   "Prepend (var value) pair to hide-ifdef-env."
  316.   (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
  317.  
  318.  
  319. (defun hif-lookup (var)
  320. ;  (message "hif-lookup %s" var)
  321.   (let ((val (assoc var hide-ifdef-env)))
  322.     (if val
  323.     (cdr val)
  324.       hif-undefined-symbol)))
  325.  
  326. (defun hif-defined (var)
  327.   (hif-lookup var)
  328.   ; when #if expressions are fully supported, defined result should be 1
  329.   ;  (if (assoc var  hide-ifdef-env)
  330.   ;      1
  331.   ;    nil)
  332. )
  333.  
  334.  
  335. ;===%%SF%% evaluation (End)  ===
  336.  
  337.  
  338.  
  339. ;===%%SF%% parsing (Start)  ===
  340. ;;;  The code that understands what ifs and ifdef in files look like.
  341.  
  342. (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
  343. (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
  344. (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
  345. (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
  346. (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
  347. (defconst hif-ifx-else-endif-regexp
  348.   (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
  349.  
  350.  
  351. (defun hif-infix-to-prefix (token-list)
  352.   "Convert list of tokens in infix into prefix list"
  353. ;  (message "hif-infix-to-prefix: %s" token-list)
  354.   (if (= 1 (length token-list))
  355.       (` (hif-lookup (quote (, (car token-list)))))
  356.     (hif-parse-if-exp token-list))
  357.   )
  358.  
  359. ; pattern to match initial identifier, !, &&, ||, (, or ).
  360. (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
  361. (defconst hif-end-of-comment "\\*/")
  362.  
  363.  
  364. (defun hif-tokenize (expr-string)
  365.   "Separate string into a list of tokens"
  366.   (let ((token-list nil)
  367.     (expr-start 0)
  368.     (expr-length (length expr-string)))
  369.  
  370.     (while (< expr-start expr-length) 
  371. ;      (message "expr-start = %d" expr-start) (sit-for 1)
  372.       (cond
  373.     ((string-match "^[ \t]+" expr-string expr-start)
  374.        ; skip whitespace
  375.      (setq expr-start (match-end 0))
  376.      ; stick newline in string so ^ matches on the next string-match
  377.      (aset expr-string (1- expr-start) ?\n)
  378.      )
  379.  
  380.     ((string-match "^/\\*" expr-string expr-start)
  381.      (setq expr-start (match-end 0))
  382.      (aset expr-string (1- expr-start) ?\n)
  383.      (or
  384.        (string-match hif-end-of-comment
  385.              expr-string expr-start) ; eat comment
  386.        (string-match "$" expr-string expr-start)) ; multi-line comment
  387.      (setq expr-start (match-end 0))
  388.      (aset expr-string (1- expr-start) ?\n)
  389.      )
  390.  
  391.     ((string-match hif-token-regexp expr-string expr-start)
  392.       (let ((token (substring expr-string expr-start (match-end 0))))
  393.         (setq expr-start (match-end 0))
  394.         (aset expr-string (1- expr-start) ?\n)
  395. ;        (message "token: %s" token) (sit-for 1)
  396.         (setq token-list
  397.           (cons
  398.             (cond
  399.               ((string-equal token "||") 'or)
  400.               ((string-equal token "&&") 'and)
  401.               ((string-equal token "!")  'not)
  402.               ((string-equal token "defined") 'hif-defined)
  403.               ((string-equal token "(") 'lparen)
  404.               ((string-equal token ")") 'rparen)
  405.               (t (intern token)))
  406.             token-list))
  407.         ))
  408.       (t (error "Bad #if expression: %s" expr-string))
  409.       ))
  410.     (nreverse token-list)
  411.     ))
  412.  
  413. ;;;-----------------------------------------------------------------
  414. ;;; Translate C preprocessor #if expressions using recursive descent.
  415. ;;; This parser is limited to the operators &&, ||, !, and "defined".
  416.  
  417. (defun hif-parse-if-exp (token-list)
  418.   "Parse the TOKEN-LIST.  Return translated list in prefix form."
  419.   (hif-nexttoken)
  420.   (prog1
  421.       (hif-expr)
  422.     (if token ; is there still a token?
  423.     (error "Error: unexpected token: %s" token)))
  424.   )
  425.  
  426. (defun hif-nexttoken ()
  427.   "Pop the next token from token-list into the let variable \"token\"."
  428.   (setq token (car token-list))
  429.   (setq token-list (cdr token-list))
  430.   token
  431.   )
  432.  
  433. (defun hif-expr ()
  434.   "Parse and expression of the form
  435.        expr : term | expr '||' term."
  436.   (let ((result (hif-term)))
  437.     (while (eq  token 'or)
  438.       (hif-nexttoken)
  439.       (setq result (list 'or result (hif-term))))
  440.   result
  441.   ))
  442.  
  443. (defun hif-term ()
  444.   "Parse a term of the form
  445.        term : factor | term '&&' factor."
  446.   (let ((result (hif-factor)))
  447.     (while (eq token 'and)
  448.       (hif-nexttoken)
  449.       (setq result (list 'and result (hif-factor))))
  450.     result
  451.     ))
  452.  
  453. (defun hif-factor ()
  454.   "Parse a factor of the form
  455.        factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
  456.   (cond
  457.     ((eq token 'not)
  458.      (hif-nexttoken)
  459.      (list 'not (hif-factor)))
  460.  
  461.     ((eq token 'lparen)
  462.      (hif-nexttoken)
  463.      (let ((result (hif-expr)))
  464.        (if (not (eq token 'rparen))
  465.        (error "Bad token in parenthesized expression: %s" token)
  466.      (hif-nexttoken)
  467.      result)))
  468.  
  469.     ((eq token 'hif-defined)
  470.      (hif-nexttoken)
  471.      (if (not (eq token 'lparen))
  472.      (error "Error: expected \"(\" after \"define\""))
  473.      (hif-nexttoken)
  474.      (let ((ident token))
  475.        (if (memq token '(or and not hif-defined lparen rparen))
  476.        (error "Error: unexpected token: %s" token))
  477.        (hif-nexttoken)
  478.        (if (not (eq token 'rparen))
  479.        (error "Error: expected \")\" after identifier"))
  480.        (hif-nexttoken)
  481.        (` (hif-defined (quote (, ident))))
  482.        ))
  483.  
  484.     (t ; identifier
  485.       (let ((ident token))
  486.     (if (memq ident '(or and))
  487.         (error "Error: missing identifier"))
  488.     (hif-nexttoken)
  489.     (` (hif-lookup (quote (, ident))))
  490.     ))
  491.  
  492.     ))
  493.  
  494. ;;;----------- end of parser -----------------------
  495.  
  496.  
  497. (defun hif-canonicalize ()
  498.   "When at beginning of #ifX, returns a canonical (evaluatable)
  499.        form for the expression."
  500.   (save-excursion
  501.     (let ((negate (looking-at hif-ifndef-regexp)))
  502.       (re-search-forward hif-ifx-regexp)
  503.       (let* ((expr-string
  504.           (buffer-substring (point)
  505.                 (progn (skip-chars-forward "^\n\r") (point))))
  506.          (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
  507. ;    (message "hif-canonicalized: %s" expr)
  508.     (if negate
  509.         (list 'not expr)
  510.       expr)))))
  511.  
  512.  
  513. (defun hif-find-any-ifX ()
  514.   "Position at beginning of next #if, #ifdef, or #ifndef, including one on
  515. this line."
  516. ;  (message "find ifX at %d" (point))
  517.   (prog1
  518.       (re-search-forward hif-ifx-regexp (point-max) t)
  519.     (beginning-of-line)))
  520.  
  521.  
  522. (defun hif-find-next-relevant ()
  523.   "Position at beginning of next #ifdef, #ifndef, #else, #endif,
  524. NOT including one on this line."
  525. ;  (message "hif-find-next-relevant at %d" (point))
  526.   (end-of-line)
  527.   ; avoid infinite recursion by only going to beginning of line if match found
  528.   (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
  529.       (beginning-of-line))
  530.   )
  531.  
  532. (defun hif-find-previous-relevant ()
  533.   "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
  534. NOT including one on this line."
  535. ;  (message "hif-find-previous-relevant at %d" (point))
  536.   (beginning-of-line)
  537.   ; avoid infinite recursion by only going to beginning of line if match found
  538.   (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
  539.      (beginning-of-line)
  540.     )
  541.   )
  542.  
  543.  
  544. (defun hif-looking-at-ifX ()        ;; Should eventually see #if
  545.   (looking-at hif-ifx-regexp))
  546. (defun hif-looking-at-endif ()
  547.   (looking-at hif-endif-regexp))
  548. (defun hif-looking-at-else ()
  549.   (looking-at hif-else-regexp))
  550.  
  551.  
  552.  
  553. (defun hif-ifdef-to-endif ()
  554.   "If positioned at #ifX or #else form, skip to corresponding #endif."
  555. ;  (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
  556.   (hif-find-next-relevant)
  557.   (cond ((hif-looking-at-ifX)
  558.      (hif-ifdef-to-endif) ; find endif of nested if
  559.      (hif-ifdef-to-endif)) ; find outer endif or else
  560.     ((hif-looking-at-else)
  561.      (hif-ifdef-to-endif)) ; find endif following else
  562.     ((hif-looking-at-endif)
  563.      'done)
  564.     (t
  565.      (error "Missmatched #ifdef #endif pair"))
  566.     ))
  567.  
  568.  
  569. (defun hif-endif-to-ifdef ()
  570.   "If positioned at #endif form, skip backward to corresponding #ifX."
  571. ;  (message "hif-endif-to-ifdef at %d" (point))
  572.   (let ((start (point)))
  573.     (hif-find-previous-relevant)
  574.     (if (= start (point))
  575.     (error "Missmatched #ifdef #endif pair")))
  576.   (cond ((hif-looking-at-endif)
  577.      (hif-endif-to-ifdef) ; find beginning of nested if
  578.      (hif-endif-to-ifdef)) ; find beginning of outer if or else
  579.     ((hif-looking-at-else)
  580.      (hif-endif-to-ifdef))
  581.     ((hif-looking-at-ifX)
  582.      'done)
  583.     (t ; never gets here
  584.      )))
  585.  
  586.  
  587. (defun forward-ifdef (&optional arg)
  588.   "Move point to beginning of line of the next ifdef-endif.
  589.        With argument, do this that many times."
  590.   (interactive "p")
  591.   (or arg (setq arg 1))
  592.   (if (< arg 0)
  593.       (backward-ifdef (- arg)))
  594.   (while (< 0 arg)
  595.     (setq arg (- arg))
  596.     (let ((start (point)))
  597.       (if (not (hif-looking-at-ifX))
  598.       (hif-find-next-relevant))
  599.       (if (hif-looking-at-ifX)
  600.       (hif-ifdef-to-endif)
  601.     (goto-char start)
  602.     (error "No following #ifdef")
  603.     ))))
  604.  
  605.  
  606. (defun backward-ifdef (&optional arg)
  607.   "Move point to beginning of the previous ifdef-endif.
  608.        With argument, do this that many times."
  609.   (interactive "p")
  610.   (or arg (setq arg 1))
  611.   (if (< arg 0)
  612.       (forward-ifdef (- arg)))
  613.   (while (< 0 arg)
  614.     (setq arg (1- arg))
  615.     (beginning-of-line)
  616.     (let ((start (point)))
  617.       (if (not (hif-looking-at-endif))
  618.       (hif-find-previous-relevant))
  619.       (if (hif-looking-at-endif)
  620.       (hif-endif-to-ifdef)
  621.     (goto-char start)
  622.     (error "No previous #ifdef")
  623.     ))))
  624.  
  625.  
  626.  
  627. (defun down-ifdef ()
  628.   "Move point to beginning of nested ifdef or else-part."
  629.     (interactive)
  630.     (let ((start (point)))
  631.       (hif-find-next-relevant)
  632.       (if (or (hif-looking-at-ifX) (hif-looking-at-else))
  633.       ()
  634.     (goto-char start)
  635.     (error "No following #ifdef")
  636.     )))
  637.  
  638.  
  639. (defun up-ifdef ()
  640.   "Move point to beginning of enclosing ifdef or else-part."
  641.   (interactive)
  642.   (beginning-of-line)
  643.   (let ((start (point)))
  644.     (if (not (hif-looking-at-endif))
  645.     (hif-find-previous-relevant))
  646.     (if (hif-looking-at-endif)
  647.     (hif-endif-to-ifdef))
  648.       (if (= start (point))
  649.       (error "No previous #ifdef")
  650.     )))
  651.  
  652. (defun next-ifdef (&optional arg)
  653.   "Move to the beginning of the next #ifX, #else, or #endif.
  654.        With argument, do this that many times."
  655.   (interactive "p")
  656.   (or arg (setq arg 1))
  657.   (if (< arg 0)
  658.       (previous-ifdef (- arg)))
  659.   (while (< 0 arg)
  660.     (setq arg (1- arg))
  661.     (hif-find-next-relevant)
  662.     (if (eolp)
  663.     (progn
  664.       (beginning-of-line)
  665.       (error "No following #ifdefs, #elses, or #endifs")
  666.       ))))
  667.  
  668. (defun previous-ifdef (&optional arg)
  669.   "Move to the beginning of the previous #ifX, #else, or #endif.
  670.        With argument, do this that many times."
  671.   (interactive "p")
  672.   (or arg (setq arg 1))
  673.   (if (< arg 0)
  674.       (next-ifdef (- arg)))
  675.   (while (< 0 arg)
  676.     (setq arg (1- arg))
  677.     (let ((start (point)))
  678.       (hif-find-previous-relevant)
  679.       (if (= start (point))
  680.       (error "No previous #ifdefs, #elses, or #endifs")
  681.     ))))
  682.  
  683.  
  684. ;===%%SF%% parsing (End)  ===
  685.  
  686.  
  687. ;===%%SF%% hide-ifdef-hiding (Start)  ===
  688.  
  689.  
  690. ;;; A range is a structure with four components:
  691. ;;; ELSE-P    True if there was an else clause for the ifdef.
  692. ;;; START    The start of the range. (beginning of line)
  693. ;;; ELSE    The else marker (beginning of line)
  694. ;;;            Only valid if ELSE-P is true.
  695. ;;; END        The end of the range.  (beginning of line)
  696.  
  697. (defun hif-make-range (else-p start end &optional else)
  698.   (list else-p start else end))
  699.  
  700. (defun hif-range-else-p (range)  (elt range 0))
  701. (defun hif-range-start (range) (elt range 1))
  702. (defun hif-range-else (range) (elt range 2))
  703. (defun hif-range-end (range) (elt range 3))
  704.  
  705.  
  706.  
  707. ;;; Find-Range
  708. ;;; The workhorse, it delimits the #if region.  Reasonably simple:
  709. ;;; Skip until an #else or #endif is found, remembering positions.  If
  710. ;;; an #else was found, skip some more, looking for the true #endif.
  711.  
  712. (defun hif-find-range ()
  713.   "Returns a Range structure describing the current #if region.
  714. Point is left unchanged."
  715. ;  (message "hif-find-range at %d" (point))
  716.   (save-excursion
  717.     (beginning-of-line)
  718.     (let ((start (point))
  719.       (else-p nil)
  720.       (else nil)
  721.       (end nil))
  722.       ;; Part one.  Look for either #endif or #else.
  723.       ;; This loop-and-a-half dedicated to E. Dijkstra.
  724.       (hif-find-next-relevant)
  725.       (while (hif-looking-at-ifX)        ; Skip nested ifdef
  726.     (hif-ifdef-to-endif)
  727.     (hif-find-next-relevant))
  728.       ;; Found either a #else or an #endif.
  729.       (cond ((hif-looking-at-else)
  730.          (setq else-p t)
  731.          (setq else (point)))
  732.         (t
  733.          (setq end (point)) ; (save-excursion (end-of-line) (point))
  734.          ))
  735.       ;; If found #else, look for #endif.
  736.       (if else-p
  737.       (progn
  738.         (hif-find-next-relevant)
  739.         (while (hif-looking-at-ifX)    ; Skip nested ifdef
  740.           (hif-ifdef-to-endif)
  741.           (hif-find-next-relevant))
  742.         (if (hif-looking-at-else)
  743.         (error "Found two elses in a row?  Broken!"))
  744.         (setq end (point))  ; (save-excursion (end-of-line) (point))
  745.         ))
  746.       (hif-make-range else-p start end else))))
  747.  
  748.       
  749. ;;; A bit slimy.
  750. ;;; NOTE:  If there's an #ifdef at the beginning of the file, we can't
  751. ;;; hide it.  There's no previous newline to replace.  If we added
  752. ;;; one, we'd throw off all the counts.  Feh.
  753.  
  754. (defun hif-hide-line (point)
  755.   "Hide the line containing point.  Does nothing if
  756. hide-ifdef-lines is nil."
  757.   (if hide-ifdef-lines
  758.       (save-excursion
  759.     (goto-char point)
  760.     (let ((modp (buffer-modified-p)))
  761.       (unwind-protect
  762.           (progn
  763.         (beginning-of-line)
  764.         (if (not (= (point) 1))
  765.             (hide-ifdef-region (1- (point)) (point))))
  766.         (set-buffer-modified-p modp))
  767.       ))
  768.     ))
  769.           
  770.  
  771. ;;;  Hif-Possibly-Hide
  772. ;;;  There are four cases.  The #ifX expression is "taken" if it
  773. ;;;  the hide-ifdef-evaluator returns T.  Presumably, this means the code
  774. ;;;  inside the #ifdef would be included when the program was
  775. ;;;  compiled.  
  776. ;;;
  777. ;;;  Case 1:  #ifX taken, and there's an #else.
  778. ;;;    The #else part must be hidden.  The #if (then) part must be
  779. ;;;    processed for nested #ifX's.
  780. ;;;  Case 2:  #ifX taken, and there's no #else.
  781. ;;;    The #if part must be processed for nested #ifX's.
  782. ;;;  Case 3:  #ifX not taken, and there's an #else.
  783. ;;;    The #if part must be hidden.  The #else part must be processed
  784. ;;;    for nested #ifs.
  785. ;;;  Case 4:  #ifX not taken, and there's no #else.
  786. ;;;    The #ifX part must be hidden.
  787. ;;;
  788. ;;;  Further processing is done by narrowing to the relevant region
  789. ;;;  and just recursively calling hide-ifdef-guts.
  790. ;;;
  791. ;;;  When hif-possibly-hide returns, point is at the end of the
  792. ;;;  possibly-hidden range.
  793.  
  794. (defun hif-recurse-on (start end)
  795.   "Call hide-ifdef-guts after narrowing to end of START line and END
  796. line."
  797.   (save-excursion
  798.     (save-restriction
  799.       (goto-char start)
  800.       (end-of-line)
  801.       (narrow-to-region (point) end)
  802.       (hide-ifdef-guts))))
  803.  
  804. (defun hif-possibly-hide ()
  805.   "Called at #ifX expression, this hides those parts that should be
  806. hidden, according to judgement of hide-ifdef-evaluator."
  807. ;  (message "hif-possibly-hide") (sit-for 1)
  808.     (let ((test (hif-canonicalize))
  809.       (range (hif-find-range)))
  810. ;      (message "test = %s" test) (sit-for 1)
  811.       
  812.       (hif-hide-line (hif-range-end range))
  813.       (if (funcall hide-ifdef-evaluator test)
  814.       (cond ((hif-range-else-p range) ; case 1
  815.          (hif-hide-line (hif-range-else range))
  816.          (hide-ifdef-region (hif-range-else range) 
  817.                     (1- (hif-range-end range)))
  818.          (hif-recurse-on (hif-range-start range)
  819.                  (hif-range-else range)))
  820.         (t ; case 2
  821.          (hif-recurse-on (hif-range-start range)
  822.                  (hif-range-end range))))
  823.     (cond ((hif-range-else-p range) ; case 3
  824.            (hif-hide-line (hif-range-else range))
  825.            (hide-ifdef-region (hif-range-start range)
  826.                   (1- (hif-range-else range)))
  827.            (hif-recurse-on (hif-range-else range)
  828.                    (hif-range-end range)))
  829.           (t ; case 4
  830.            (hide-ifdef-region (point)
  831.                   (1- (hif-range-end range))))
  832.           ))
  833.       (hif-hide-line (hif-range-start range))    ; Always hide start.
  834.       (goto-char (hif-range-end range))
  835.       (end-of-line)
  836.       ))
  837.  
  838.  
  839.  
  840. (defun hide-ifdef-guts ()
  841.   "Does the work of hide-ifdefs, except for the work that's pointless
  842. to redo on a recursive entry."
  843. ;  (message "hide-ifdef-guts")
  844.   (save-excursion
  845.     (goto-char (point-min))
  846.     (while (hif-find-any-ifX)
  847.       (hif-possibly-hide))))
  848.  
  849. ;===%%SF%% hide-ifdef-hiding (End)  ===
  850.  
  851.  
  852. ;===%%SF%% exports (Start)  ===
  853.  
  854. (defvar hide-ifdef-initially nil
  855.   "*Non-nil if hide-ifdefs should be called when hide-ifdef-mode
  856.     is first activated.")
  857.  
  858. (defvar hide-ifdef-hiding nil
  859.   "Non-nil if text might be hidden.")
  860.  
  861. (defvar hide-ifdef-read-only nil
  862.   "*Set to non-nil if you want buffer to be read-only while hiding text.")
  863.  
  864. (defvar hif-outside-read-only nil
  865.   "Internal variable.  Saves the value of buffer-read-only while hiding.")
  866.  
  867. (defvar hide-ifdef-lines nil
  868.   "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
  869.  
  870. (defun hide-ifdef-toggle-read-only ()
  871.   "Toggle hide-ifdef-read-only."
  872.   (interactive)
  873.   (setq hide-ifdef-read-only (not hide-ifdef-read-only))
  874.   (message "Hide-Read-Only %s"
  875.        (if hide-ifdef-read-only "ON" "OFF"))
  876.   (if hide-ifdef-hiding
  877.       (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
  878.   (hif-update-mode-line)
  879.   )
  880.  
  881. (defun hide-ifdef-toggle-outside-read-only ()
  882.   "Replacement for toggle-read-only within hide-ifdef-mode."
  883.   (interactive)
  884.   (setq hif-outside-read-only (not hif-outside-read-only))
  885.   (message "Read only %s"
  886.        (if hif-outside-read-only "ON" "OFF"))
  887.   (setq buffer-read-only
  888.     (or (and hide-ifdef-hiding hide-ifdef-read-only)
  889.         hif-outside-read-only)
  890.     )
  891.   (hif-update-mode-line)
  892.   )
  893.  
  894.       
  895. (defun hide-ifdef-define (var)
  896.   "Define a VAR so that #ifdef VAR would be included."
  897.   (interactive "SDefine what? ")
  898.   (hif-set-var var t)
  899.   (if hide-ifdef-hiding (hide-ifdefs)))
  900.  
  901. (defun hide-ifdef-undef (var)
  902.   "Undefine a VAR so that #ifdef VAR would not be included."
  903.   (interactive "SUndefine what? ")
  904.   (hif-set-var var nil)
  905.   (if hide-ifdef-hiding (hide-ifdefs)))
  906.  
  907.  
  908. (defun hide-ifdefs ()
  909.   "Hide the contents of some #ifdefs.  Assume that defined symbols have
  910. been added to hide-ifdef-env.  The text hidden is the text that would not
  911. be included by the C preprocessor if it were given the file with those
  912. symbols defined.
  913.  
  914. Turn off hiding by calling show-ifdef."
  915.  
  916.   (interactive)
  917.   (message "Hiding...")
  918.   (if (not hide-ifdef-mode)
  919.       (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
  920.   (if hide-ifdef-hiding
  921.       (show-ifdefs))            ; Otherwise, deep confusion.
  922.   (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
  923.   (setq selective-display t)
  924.   (setq hide-ifdef-hiding t)
  925.   (hide-ifdef-guts)
  926.   (if (or hide-ifdef-read-only hif-outside-read-only)
  927.       (toggle-read-only) ; make it read only
  928.     )
  929.   (message "Hiding done")
  930.   )
  931.  
  932.  
  933. (defun show-ifdefs ()
  934.   "Cancel the effects of hide-ifdef.  The contents of all #ifdefs is shown."
  935.   (interactive)
  936.   (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
  937.   (setq selective-display nil)    ; defaults
  938.   (hif-show-all)
  939.   (if hif-outside-read-only
  940.       (toggle-read-only)) ; make it read only
  941.   (setq hide-ifdef-hiding nil)
  942.   )
  943.  
  944.  
  945. (defun hif-find-ifdef-block ()
  946.   "Utilitiy for hide and show ifdef-block.  Set top and bottom of ifdef block."
  947.   (let (max-bottom)
  948.   (save-excursion
  949.     (beginning-of-line)
  950.     (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
  951.     (up-ifdef))
  952.     (setq top (point))
  953.     (hif-ifdef-to-endif)
  954.     (setq max-bottom (1- (point)))
  955.     )
  956.   (save-excursion
  957.     (beginning-of-line)
  958.     (if (not (hif-looking-at-endif))
  959.     (hif-find-next-relevant))
  960.     (while (hif-looking-at-ifX)
  961.       (hif-ifdef-to-endif)
  962.       (hif-find-next-relevant)
  963.       )
  964.     (setq bottom (min max-bottom (1- (point))))
  965.     ))
  966.   )
  967.  
  968.  
  969. (defun hide-ifdef-block ()
  970.   "Hide the ifdef block (true or false part) enclosing or before the cursor."
  971.   (interactive)
  972.   (if (not hide-ifdef-mode)
  973.       (hide-ifdef-mode 1))
  974.   (if buffer-read-only (toggle-read-only))
  975.   (setq selective-display t)
  976.   (let (top bottom)
  977.     (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
  978.     (hide-ifdef-region top bottom)
  979.     (if hide-ifdef-lines
  980.     (progn
  981.       (hif-hide-line top)
  982.       (hif-hide-line (1+ bottom))))
  983.     (setq hide-ifdef-hiding t)
  984.     )
  985.   (if (or hide-ifdef-read-only hif-outside-read-only)
  986.       (toggle-read-only))
  987.   )
  988.  
  989.  
  990. (defun show-ifdef-block ()
  991.   "Show the ifdef block (true or false part) enclosing or before the cursor."
  992.   (interactive)
  993.   (let ((old-read-only buffer-read-only))
  994.     (if old-read-only (toggle-read-only))
  995.     (if hide-ifdef-lines
  996.     (save-excursion
  997.       (beginning-of-line)
  998.       (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
  999.  
  1000.       (let (top bottom)
  1001.     (hif-find-ifdef-block)
  1002.     (hif-show-ifdef-region (1- top) bottom))
  1003.       )
  1004.  
  1005.     ; restore read only status since we dont know if all is shown.
  1006.     (if old-read-only (toggle-read-only))
  1007.     ))
  1008.  
  1009.  
  1010.  
  1011. ;;;  defininition alist support
  1012.  
  1013. (defvar hide-ifdef-define-alist nil
  1014.   "A global assoc list of pre-defined symbol lists")
  1015.  
  1016. (defun hif-compress-define-list (env)
  1017.   "Compress the define list ENV into a list of defined symbols only."
  1018.   (let ((defs (mapcar '(lambda (arg)
  1019.              (if (hif-lookup (car arg)) (car arg)))
  1020.               env))
  1021.     (new-defs nil))
  1022.     (while defs
  1023.       (if (car defs)
  1024.       (setq new-defs (cons (car defs) new-defs)))
  1025.       (setq defs (cdr defs)))
  1026.     new-defs
  1027.     ))
  1028.  
  1029. (defun hide-ifdef-set-define-alist (name)
  1030.   "Set the association for NAME to hide-ifdef-env."
  1031.   (interactive "SSet define list: ")
  1032.   (setq hide-ifdef-define-alist
  1033.     (cons (cons name (hif-compress-define-list hide-ifdef-env))
  1034.           hide-ifdef-define-alist))
  1035.   )
  1036.  
  1037. (defun hide-ifdef-use-define-alist (name)
  1038.   "Set hide-ifdef-env to the define list specified by NAME."
  1039.   (interactive "SUse define list: ")
  1040.   (let ((define-list (assoc name hide-ifdef-define-alist)))
  1041.     (if define-list
  1042.     (setq hide-ifdef-env
  1043.           (mapcar '(lambda (arg) (cons arg t))
  1044.               (cdr define-list)))
  1045.       (error "No define list for %s" name))
  1046.     (if hide-ifdef-hiding (hide-ifdefs))
  1047.     )
  1048.   )
  1049.  
  1050. ;===%%SF%% exports (End)  ===
  1051.